<?php

App::uses('Crud', 'Model');
App::uses('UserArea', 'Model');
App::uses('Area', 'Model');
App::uses('Moe', 'Model');
App::uses('Categorization', 'Model');

class Test extends Crud
{
    /** Al Agregar/eliminar/actualizar las constantes de los modelos implica ir
     * y actualizar los comentarios en los campos de la tabla asociada a este modelo.
     */
    const STATUS_ENABLED = 1;
    const STATUS_DISABLED = 0;

    const TYPE_GENERAL = 1;
    const TYPE_LEVEL = 2;

    /**
     * Obtenemos los tipo de examenes
     * @return array
     */
    public static function getTypes()
    {
        return array(
            array(
                'value' => self::TYPE_GENERAL,
                'text' => 'General',
            ),
            array(
                'value' => self::TYPE_LEVEL,
                'text' => 'Nivel',
            )
        );
    }

    /**
     * obtenemos los examenes habilitados
     * @return array
     */
    public function getEnabled($testId = null)
    {
        $filters = array(
            'fields' => array(
                'id as value',
                'name as text',
            ),
            'order' => array(
                'name' => 'ASC'
            ),
            'conditions' => array(
                'status' => self::STATUS_ENABLED
            ),
        );
        if ($testId !== null) {
            $filters['conditions']['id'] = $testId;
        }
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }

    /**
     * Buscamos un area por su nombre
     * @param string $name
     * @return array
     */
    public function getByName($name)
    {
        return $this->find('first', array('conditions' => array(
            'name' => $name
        )));
    }

    /**
     * Obtenemos los cursos activos
     * @return array
     */
    public function getEnabledByArea()
    {
        $filters = array(
            'fields' => array(
                'id',
                'id as value',
                'name',
                'name as text',
                'attempts',
                'type',
                'min_capacity',
                'lock_days',
                'minimum_qualification'
            ),
            'conditions' => array(
                'status' => self::STATUS_ENABLED,
            ),
            'order' => array(
                'name' => 'ASC'
            ),
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }

    /**
     * Buscamos un curso por su nombre y su linea, auqneu en realidad
     * los cursos son por area.
     * @param string $name
     * @param string $lineId UUID
     * @return array
     */
    public function getByNameAndLine($name, $lineId)
    {
        $filters = array(
            'fields' => array('Test.id'),
            'joins' => array(
                array(
                    'table' => 'lines',
                    'alias' => 'l',
                    'type' => 'INNER',
                    'conditions' => array(
                        'l.area_id = Test.area_id',
                        'l.status' => Line::STATUS_ENABLED,
                    ),
                ),
            ),
            'conditions' => array(
                'Test.name' => $name,
                'l.id' => $lineId,
            ),
        );
        return $this->find('first', $filters);
    }

    public function getForScheduler()
    {
        $filters = array(
            'fields' => array(
                'Test.id as value',
                'Test.name as text',
                'IF(c.colour IS NULL, "#50abc2", c.colour) as color'
            ),
            'joins' => array(
                array(
                    'table' => 'categorizations',
                    'alias' => 'c',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'c.test_id = Test.id',
                        'c.status' => Categorization::STATUS_ENABLED
                    )
                )
            ),
            'conditions' => array(
                'Test.status' => self::STATUS_ENABLED
            ),
            'group' => array(
                'Test.id',
                'Test.name',
                'c.colour'
            ),
            'order' => array(
                'Test.name',
            )
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }

    public function getTestDescriptionById($testId)
    {
        $filters = array(
            'fields' => array(
                'Test.name as text',
                'IF(c.colour IS NULL, "#50abc2", c.colour) as color'
            ),
            'joins' => array(
                array(
                    'table' => 'categorizations',
                    'alias' => 'c',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'c.test_id = Test.id',
                        'c.status' => Categorization::STATUS_ENABLED
                    )
                )
            ),
            'conditions' => array(
                'Test.id' => $testId,
                'Test.status' => self::STATUS_ENABLED
            ),
        );
        return $this->find('first', $filters);
    }

    public function getEnabledById($testId)
    {
        $filters = array(
            'fields' => array(
                'attempts',
                'minimum_qualification',
                'lock_days'
            ),
            'conditions' => array(
                'id' => $testId,
                'status' => self::STATUS_ENABLED
            ),
        );
        return $this->find('first', $filters);
    }
}
